package com.cool.controller;

import com.cool.dao.ReserveMapper;
import com.cool.entity.Parkinglot;
import com.cool.entity.Reserve;
import com.cool.service.ParkinglotService;
import com.cool.service.ReserveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.ArrayList;

@RestController
@RequestMapping(value = "/api/reserves")
public class ReserveController {
    @Autowired
    private ReserveService reserveService;

    @Autowired
    private ReserveMapper reserveMapper;

    @Autowired
    private ParkinglotService parkinglotService;

    @RequestMapping(method = RequestMethod.POST,consumes = "application/json;charset=utf-8")
    public ResponseEntity<Reserve> reserve(@RequestBody Reserve reserve)
    {
        System.out.println(reserve.getUserid());
        int uid=reserve.getUserid();
        if(reserveService.isPayless(uid)==true){
            Reserve reserves=new Reserve();
            reserves=reserveService.payless(uid);
            return new ResponseEntity<Reserve>(reserves,HttpStatus.ACCEPTED);
        }
        if(reserveService.isReserving(uid)==true){
            Reserve reserves=new Reserve();
            reserves=reserveService.reserving(uid);
            return new ResponseEntity<Reserve>(reserves,HttpStatus.CREATED);
        }
        Parkinglot parkinglot=parkinglotService.selectById(reserve.getPlid());
        if(parkinglotService.subSurplus(parkinglot)==-1){
            return new ResponseEntity<Reserve>(HttpStatus.NO_CONTENT);
        }
            reserveService.insert(reserve);
            ResponseEntity<Reserve> responseEntity = new ResponseEntity<Reserve>(reserve,HttpStatus.OK);
            return responseEntity;
        }

        @RequestMapping(value="/{id}",method = RequestMethod.DELETE,produces = "application/json;charset=utf-8")
        public ResponseEntity<Reserve> delReserve(@PathVariable int id)
        {
                Reserve reserve=reserveMapper.selectByPrimaryKey(id);
                if(reserve==null||reserve.getStatus()!=1){
                    return new ResponseEntity<Reserve>(HttpStatus.NO_CONTENT);
                }
                    Parkinglot parkinglot=parkinglotService.selectById(reserve.getPlid());
                    reserveMapper.deleteByPrimaryKey(id);
                    parkinglotService.addSurplus(parkinglot);
                    return new ResponseEntity<Reserve>(HttpStatus.OK);
        }

        @RequestMapping(method = RequestMethod.PUT,consumes = "application/json;charset=utf-8",produces = "application/json;charset=utf-8")
        public ResponseEntity<Reserve> update(@RequestBody Reserve reserve){
                int id=reserve.getId();
                Reserve result=reserveMapper.selectByPrimaryKey(id);
                if(result==null){
                    return new ResponseEntity<Reserve>(HttpStatus.NO_CONTENT);
                }
                reserveMapper.updateByPrimaryKeySelective(reserve);
                return new ResponseEntity<Reserve>(HttpStatus.OK);

        }

        @RequestMapping(value = "/{id}",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
        public ResponseEntity<ArrayList<Reserve>> getAll(@PathVariable int id)
        {
            ArrayList<Reserve> reserves=reserveMapper.selectByUid(id);
            return new ResponseEntity<ArrayList<Reserve>>(reserves,HttpStatus.OK);
        }

}
